//
//  ClassifyViewController.m
//  TodatPlay
//
//  Created by dllo on 16/9/10.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ClassifyViewController.h"
#import "ClassifyCollectionViewCell.h"
#import "TAMacro.h"
#import "CDMNetTool.h"
// model
#import "ClassifyRootClass.h"
#import "ClassifyData.h"
#import "ClassifyTimedoc.h"
#import "ClassifyOrderdoc.h"
#import "ClassifyCategorydoc.h"
#import "ClassifyToday.h"
#import "ClassifySunday.h"


#import "TitleView.h"
#import "UIView+Frame.h"
#import "TitleCollectionViewCell.h"

#define KCollectioViewReuse @"reuse"
@interface ClassifyViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *contentsCollectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *contentsLayout;
@property (nonatomic, strong) ClassifyRootClass *rootClass;


@end

@implementation ClassifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self configCollectionView];
    [self handleData];
//    [self createTitleView];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"left_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(handleLeft:)];
}
- (void)handleLeft:(UIBarButtonItem *)btn{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)configCollectionView {
    [self.contentsCollectionView registerNib:[UINib nibWithNibName:@"ClassifyCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:KCollectioViewReuse];
    self.contentsLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.contentsLayout.minimumLineSpacing = 0;
    self.contentsLayout.minimumInteritemSpacing = 0;
    self.contentsLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.contentsCollectionView.bounces = NO;
    self.contentsCollectionView.pagingEnabled = YES;
}
- (void)createTitleView {
    self.titleView = [[TitleView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 40)];
    [self.titleView didClickIndexPath:^(NSIndexPath *index) {
        [self.contentsCollectionView scrollToItemAtIndexPath:index atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
        
    }];
    _titleView.categoryArr = self.rootClass.data.CategoryDoc;
    
    _titleView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_titleView];
}
#pragma mark - handleData

- (void)handleData {
    NSString *urlString = @"http://wanapi.damai.cn/filterpnew.json?platform=4&channel_from=m360_market&version=203004&source=10345";
    [CDMNetTool GET:urlString andBody:nil andHeader:nil andResponse:CDMJSON andSuccessBlock:^(id result) {
        self.rootClass = [ClassifyRootClass modelWithDic:result];
        [self createTitleView];
        [self.contentsCollectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        NSLog(@"请求失败%@", error);
        
    }];

    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.rootClass.data.CategoryDoc.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ClassifyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:KCollectioViewReuse forIndexPath:indexPath];
    cell.cateid = self.rootClass.data.CategoryDoc[indexPath.row].ID;
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(SCREEN_WIDTH, self.contentsCollectionView.frame.size.height);
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger page = scrollView.contentOffset.x / SCREEN_WIDTH;
    NSLog(@"第%ld页", page);
    
    NSIndexPath *index = [NSIndexPath indexPathForItem:page inSection:0];
    
    TitleCollectionViewCell *cell = (TitleCollectionViewCell *)[self.titleView.titleCollectionView cellForItemAtIndexPath:index];
    self.titleView.lineView.dm_width = cell.contentView.dm_width;
    [self.titleView.titleCollectionView scrollToItemAtIndexPath:index atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
